package com.woniu.commons.log;


import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class LogAdvice {

    @Around("@annotation(com.woniu.commons.log.yogaLog)")
    public Object log(ProceedingJoinPoint pjp){

        try {
            //得到参数输出日志，记录时间,获取目标方法对象
            MethodSignature ms = (MethodSignature) pjp.getSignature();
            //获取注解
            yogaLog ml = ms.getMethod().getAnnotation(yogaLog.class);

            log.info("======执行{}业务,======参数是{}",ml.value(),pjp.getArgs());
            Long start = System.currentTimeMillis();
            Object obj = pjp.proceed();
            String result = JSONUtil.parse(obj).toString();
            log.info("======执行{}业务,======结果是{},======耗时{}",ml.value(),result,System.currentTimeMillis() - start);
            return obj;
        } catch (Throwable e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }






}
